function hfile = vmr_SaveV16(hfile, v16fname)
% VMR::SaveV16  - save matching V16 file into 16-bit VMR file
%
% FORMAT:       [vmr] = vmr.SaveV16([v16fname])
%
% Input fields:
%
%       v16fname    alternative filename, otherwise use VMR's filename
%
% Output fields:
%
%       vmr         VMR object (with possibly updated transio reference!)

% Version:  v0.7b
% Build:    7090216
% Date:     Sep-02 2007, 4:43 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'vmr')
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to ''%s''.', ...
        mfilename ...
    );
end
sbc = bvqxfile_getscont(hfile.L);
bc = sbc.C;
if strcmpi(class(bc.VMRData), 'uint16')
    error( ...
        'BVQXfile:InvalidObject', ...
        'Method only valid for 8-bit VMRs.' ...
    );
end
if nargin < 2
    [vmrfname{1:3}] = fileparts(sbc.F);
    if isempty(vmrfname{2})
        error( ...
            'BVQXfile:InvalidObject', ...
            'This method only works without arguments on loaded VMRs.' ...
        );
    end
    if strcmp(vmrfname{3}, '.VMR')
        v16fname = [vmrfname{1} '/' vmrfname{2} '.V16'];
    else
        v16fname = [vmrfname{1} '/' vmrfname{2} '.v16'];
    end
elseif ~ischar(v16fname) || ...
    numel(v16fname) < 4 || ...
    ~strcmpi(v16fname(end-3:end), '.v16')
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid V16 filename argument.' ...
    );
end
v16fname = v16fname(:)';

% check dimensions
if isempty(bc.VMRData16) || ...
    numel(size(bc.VMRData)) ~= numel(size(bc.VMRData16)) || ...
    any(size(bc.VMRData) ~= size(bc.VMRData16))
    error( ...
        'BVQXfile:InvalidObject', ...
        'Dimensions between VMRData and VMRData16 must match.' ...
    );
end

% check transio case, in which the file IS saved
if istransio(bc.VMRData16) && ...
   ((ispc && strcmpi(transiofilename(bc.VMRData16), v16fname)) || ...
    strcmp(transiofilename(bc.VMRData16), v16fname))
    return;
end

% try to save V16
try
    vmr16l = [];
    vmr16 = BVQXfile('new:v16');
    vmr16l = vmr16.L;
    vmr16c = bvqxfile_getcont(vmr16l);
    vmr16c.FileVersion = 1;
    vmr16c.DimX = size(bc.VMRData16, 1);
    vmr16c.DimY = size(bc.VMRData16, 1);
    vmr16c.DimZ = size(bc.VMRData16, 1);
    vmr16c.VMR8bit = false;
    vmr16c.VMRData = bc.VMRData16;
    bvqxfile_setcont(vmr16l, vmr16c);
    aft_SaveAs(vmr16, v16fname);
    
    % update transio reference if needed
    if istransio(vmr16c.VMRData)
        vmr16c = bvqxfile_getcont(vmr16l);
        bc.VMRData16 = vmr16c.VMRData;
        bvqxfile_setcont(hfile.L, bc);
    end
catch
    if ~isempty(vmr16l)
        bvqxfile_clear(vmr16l);
    end
    error( ...
        'BVQXfile:BadObject', ...
        'V16 file not writable.' ...
    );
end
